package dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import model.ActivityInfo;

public class ActivityDao {
	public boolean addActivity(String activityName,String activityDescription,int tickets) throws SQLException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String selectSql = "select * from activities_info where activity_name= "+"'"+activityName+"'";
        ActivityInfo actInfo = queryRunner.query(selectSql, new BeanHandler<ActivityInfo>(ActivityInfo.class));
        if(null == actInfo) {
        	String sql = "insert into activities_info(activity_name,activity_description,rest_ticket) values(?,?,?)";
            int row = queryRunner.update(sql,activityName,activityDescription,tickets);
            if(row>0) {
            	return true;
            }else {
            	return false;
            }
           
        }else {
        	return false;
        }
	}
	public List<ActivityInfo> getActivityList() throws SQLException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "select * from activities_info";
        List<ActivityInfo> activityInfo = queryRunner.query(sql, new BeanListHandler<ActivityInfo>(ActivityInfo.class));
		return activityInfo;
	}
	public boolean reduceRestTicket(String activityName) throws SQLException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "update activities_info set rest_ticket=rest_ticket-1 where activity_name=?";
        int row = queryRunner.update(sql,activityName);
        if(row>0) {
        	return true;
        }else {
        	return false;
        }
	}
}
